package com.diyunapp.xilinapp.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.EvaluationBean;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.diyunapp.xilinapp.utils.MyDateUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/21.
 */
public class EvaluationAdapter extends BaseAdapter {

    private List<EvaluationBean> myLists;
    private Context myContext;

    public EvaluationAdapter(List<EvaluationBean> myLists, Context myContext) {
        this.myLists = myLists;
        this.myContext = myContext;
    }

    @Override
    public int getCount() {
        return myLists.size();
    }

    @Override
    public Object getItem(int position) {
        return myLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(myContext).inflate(R.layout.good_evaluation_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EvaluationBean data = myLists.get(position);
        holder.evaluationUsernameTv.setText(FormatConfirm.usernameEncode(data.getEvaluateUserName()));

        int score = FormatConfirm.stringToInt(data.getEvaluateScore());
        if (score >= 4) {
            holder.evaluationScoreTv.setText("好评");
        } else if (score < 2) {
            holder.evaluationScoreTv.setText("差评");
        } else {
            holder.evaluationScoreTv.setText("中评");
        }
        Long time = Long.parseLong(data.getEvaluateTime());
        holder.evaluationTimeTv.setText(MyDateUtils.getCompleteTime(time));
        holder.evaluationDetailTv.setText(data.getEvaluateInfo());
        holder.evaluationSizeTv.setText(data.getGoodSize());
        if (data.getSalerExplain() == null || data.getSalerExplain().length() <= 0 || data.getSalerExplain().equals("null")) {

            holder.salerExplainTv.setVisibility(View.GONE);
        }
        else
        {
            holder.salerExplainTv.setVisibility(View.VISIBLE);
            holder.salerExplainTv.setText(data.getSalerExplain());
        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.evaluation_username_tv)
        TextView evaluationUsernameTv;
        @Bind(R.id.evaluation_score_tv)
        TextView evaluationScoreTv;
        @Bind(R.id.evaluation_time_tv)
        TextView evaluationTimeTv;
        @Bind(R.id.evaluation_detail_tv)
        TextView evaluationDetailTv;
        @Bind(R.id.evaluation_size_tv)
        TextView evaluationSizeTv;
        @Bind(R.id.saler_explain_tv)
        TextView salerExplainTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
